﻿using DevExpress.Web.ASPxUploadControl;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Utility1;

namespace eDream.admin
{
    public partial class Edit_New : System.Web.UI.UserControl
    {
        public string pathUrl = "";
        const string UploadDirectory = "~/Image/New/";
        protected void Page_Load(object sender, EventArgs e)
        {
            pathUrl = WebUtils.pathUrl() + "/Image/New/";
            if (!IsPostBack)
                LoadCBX();
        }

        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile, 130, 100);
        }

        string SavePostedFile(UploadedFile uploadedFile, int size, int size1)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            // Random r = new Random(9999);
            // int n = r.Next(999999);
            string ThumbnailFileName = uploadedFile.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            lbthongbao.Text = fileName;
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, size, size1))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            Session["link"] = ThumbnailFileName;
            Session.Timeout = 120;
            return ThumbnailFileName;
        }

        protected void btnOk_Click(object sender, System.EventArgs e)
        {
            if (hdfID.Value.ToString() != "")
                updateProduc();
            else
                createProduct();
        }

        void LoadCBX()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.cate_News
                           where k.flag == true
                           select new { k.ID, k.catename };
                cbloai.DataSource = list;
                cbloai.TextField = "catename";
                cbloai.ValueField = "ID";
                cbloai.DataBind();
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        void Bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();

            try
            {
                dx.Connection.Open();
                if (Request.QueryString["k"] != null)
                {
                    hdfID.Value = Request.QueryString["k"];
                    var list = from k in dx.New_s
                               where k.ID == int.Parse(Request.QueryString["k"].ToString())
                               select k;
                    foreach (New_ p in list)
                    {

                       
                        txtProductname.Text = p.title;
                       
                        cbloai.SelectedItem = cbloai.Items.FindByValue(p.cateID);
                       
                        radChitiet.Text = p.content;
                   
                        //previewImage0.Src = pathUrl+"/"+p.imagLage;
                        // previewImage.Src = pathUrl + "/" + p.image;
                        //previewImage1.Src = pathUrl + "/" + p.hinh1;
                        //previewImage2.Src = pathUrl + "/" + p.hinh2;
                        // previewImage3.Src = pathUrl + "/" + p.hinh3;
                        //previewImage4.Src = pathUrl + "/" + p.hinh4;
                        // previewImage5.Src = pathUrl + "/" + p.hinh5;
                    }
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        void createProduct()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                string chuoi = WebUtils.convertToUnSign3(txtProductname.Text);
                string chuoi1 = chuoi.Replace(" ", "_");
                dx.Connection.Open();
                New_ p = new New_();
               
                p.title = txtProductname.Text;
                //p.ipronameSing = chuoi1.Replace(".", "_").Replace("+", "").Replace("-", "").Replace("*", "");
             
                p.cateID = int.Parse(cbloai.Value.ToString());
                p.content = radChitiet.Text;
                p.mota = txtmota.Text;
       
               
                if (Session["link"] != null)
                    p.photo = Session["link"].ToString();
               
               
                p.flag = true;
                p.datepost = DateTime.Now;
                p.dateup = DateTime.Now;
               
                dx.New_s.InsertOnSubmit(p);
                dx.SubmitChanges();

                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Sản phẩm đã được cập nhật";
                txtProductname.Text = "";
                
                

                Session.Remove("link");
               
            }
            catch (Exception)
            {
                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Đã xãy ra lỗi! vui lòng thử lại. Khi thử lại không cần upload lại hình ảnh";
            }
            finally { dx.Connection.Close(); }
        }

        void updateProduc()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                string chuoi = WebUtils.convertToUnSign3(txtProductname.Text);
                string chuoi1 = chuoi.Replace(" ", "_");
                dx.Connection.Open();
                New_ p = new New_();
                p = dx.New_s.Single(l => l.ID == int.Parse(hdfID.Value));
                p.title = txtProductname.Text;
                //p.ipronameSing = chuoi1.Replace(".", "_").Replace("+", "").Replace("-", "").Replace("*", "");

                p.cateID = int.Parse(cbloai.Value.ToString());
                p.content = radChitiet.Text;
                p.mota = txtmota.Text;


                if (Session["link"] != null)
                    p.photo = Session["link"].ToString();


                p.flag = true;
               
                p.dateup = DateTime.Now;
               
                dx.SubmitChanges();

                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Sản phẩm đã được cập nhật";

               
                Session.Remove("link");
               
            }
            catch (Exception)
            {
                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Đã xãy ra lỗi! vui lòng thử lại. Khi thử lại không cần upload lại hình ảnh";
            }
            finally { dx.Connection.Close(); }
        }
    }
}